package com.appstore.controller;


import com.appstore.common.AjaxResult;
import com.appstore.common.utils.SecurityUtils;
import com.appstore.domain.entity.BrowsingHistory;
import com.appstore.service.BrowsingHistoryService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/browsing_history")
public class BrowsingHistoryController {

    @Resource
    private BrowsingHistoryService browsingHistoryService;

    @GetMapping("/userId")
    public AjaxResult getBrowsingHistoryList() {
//        if(ObjectUtils.isEmpty(userId)) return AjaxResult.error("请求参数为空");
        Long userId = SecurityUtils.getUserId();
        return AjaxResult.success(browsingHistoryService.getBrowsingHistoryList(userId));
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteBrowsingHistory(@PathVariable Long id) {
        if(ObjectUtils.isEmpty(id)) return AjaxResult.error("请求参数为空");
        return AjaxResult.success(browsingHistoryService.deleteBrowsingHistory(id));
    }

    @PostMapping
    public AjaxResult addBrowsingHistory(@RequestBody BrowsingHistory browsingHistory) {
        if(ObjectUtils.isEmpty(browsingHistory)) return AjaxResult.error("请求参数为空");
        browsingHistory.setUserId(SecurityUtils.getUserId());
        return AjaxResult.success(browsingHistoryService.addBrowsingHistory(browsingHistory));
    }
}
